package com.tomtop.core.redis.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.tomtop.core.redis.ByteRedisTemplate;
import com.tomtop.core.redis.EnableTransactionIntegerRedisTemplate;
import com.tomtop.core.redis.IntegerRedisTemplate;
import com.tomtop.core.redis.JsonBytesRedisTemplate;
import com.tomtop.core.redis.LongRedisTemplate;

@Configuration
@EnableCaching
public class RedisConfig {
	private static Logger log = LoggerFactory .getLogger(RedisConfig.class);
	
	@Bean @ConfigurationProperties("spring.redis.pool")
	Object poolConfig(JedisConnectionFactory factory){ 
		return factory.getPoolConfig(); 
	}
	
    @Bean StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { 
        StringRedisTemplate template = new StringRedisTemplate(factory);  
        template.afterPropertiesSet();  
        log.debug("初始化StringRedisTemplate 完毕");
        return template;  
    }
    
    @Bean @Primary <T> RedisTemplate<String,T> redisTemplate(RedisConnectionFactory factory) { 
    	RedisTemplate<String,T> template = new RedisTemplate<String,T>(); 
    	template.setConnectionFactory(factory);
		RedisSerializer<String> stringSerializer = new StringRedisSerializer();
		template.setKeySerializer(stringSerializer);
		template.setHashKeySerializer(stringSerializer);
        template.afterPropertiesSet();
        return template;  
    } 

    @Bean IntegerRedisTemplate integerRedisTemplate(RedisConnectionFactory factory) {  
    	IntegerRedisTemplate template = new IntegerRedisTemplate(); 
    	template.setConnectionFactory(factory);
        template.afterPropertiesSet(); 
        return template;  
    } 
    
    @Bean LongRedisTemplate longRedisTemplate(RedisConnectionFactory factory) {  
    	LongRedisTemplate template = new LongRedisTemplate(); 
    	template.setConnectionFactory(factory);
        template.afterPropertiesSet(); 
        return template;  
    } 
    
    @Bean EnableTransactionIntegerRedisTemplate enableTransactionIntegerRedisTemplate(RedisConnectionFactory factory) {  
    	EnableTransactionIntegerRedisTemplate template = new EnableTransactionIntegerRedisTemplate(); 
    	template.setConnectionFactory(factory);
        template.afterPropertiesSet(); 
        return template;  
    } 

    @Bean ByteRedisTemplate byteRedisTemplate(RedisConnectionFactory factory) {  
    	ByteRedisTemplate template = new ByteRedisTemplate(); 
    	template.setConnectionFactory(factory);
        template.afterPropertiesSet(); 
        return template;  
    } 
    @Bean JsonBytesRedisTemplate  jsonBytesRedisTemplate(RedisConnectionFactory factory) {  
    	JsonBytesRedisTemplate template = new JsonBytesRedisTemplate(); 
    	template.setConnectionFactory(factory);
        template.afterPropertiesSet(); 
        return template;  
    } 
    
	@ConfigurationProperties(prefix="spring.redis.expire")
	@Bean@Primary CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
		return  new RedisCacheManager(redisTemplate);
	}
}
